﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace xCarLinkMP3Organizer
{
    /// <summary>
    /// Interaction logic for ProgressWindow.xaml
    /// </summary>
    public partial class ProgressWindow : Window
    {
        private ManualResetEvent e_;
        private bool finished_;

        public ProgressWindow(ManualResetEvent e)
        {
            e_ = e;
            InitializeComponent();

            Loaded += new RoutedEventHandler(ProgressWindow_Loaded);
            Closing += new System.ComponentModel.CancelEventHandler(ProgressWindow_Closing);
        }

        void ProgressWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !finished_;
        }

        void ProgressWindow_Loaded(object sender, RoutedEventArgs e)
        {
            e_.Set();
        }

        public ProgressBar ProgressBar
        {
            get
            {
                return p_;
            }
        }

        public string Text
        {
            get { return t_.Text; }
            set { t_.Text = value; }
        }

        public void Finish()
        {
            finished_ = true;
            Close();
        }
    }

    public class LongOperation
    {
        ProgressWindow _pw;

        public LongOperation(int steps, Func<IEnumerable<string>> operation)
        {
            var pwEvent = new ManualResetEvent(false);
            _pw = new ProgressWindow(pwEvent);
            _pw.ProgressBar.Minimum = 0;
            _pw.ProgressBar.Maximum = steps;

            ThreadPool.QueueUserWorkItem(state =>
            {
                pwEvent.WaitOne();
                var ii = 0;
                try
                {
                    foreach (var s in operation())
                    {
                        _pw.Dispatcher.BeginInvoke(
                            (Action<int, string>)((vv, m) =>
                            {
                                _pw.ProgressBar.Value = vv;
                                _pw.Text = m;
                            }),
                            ii, s);
                        ii++;
                    }
                }
                catch (Exception e)
                {
                    _pw.Dispatcher.BeginInvoke((Action) (() =>
                                                             {
                                                                 _pw.Finish();
                                                                 MessageBox.Show(e.Message, "Error", MessageBoxButton.OK);
                                                             }));
                }

                _pw.Dispatcher.BeginInvoke((Action)_pw.Finish);
            });

            _pw.Owner = MainWindow.Instance;
        }

        public void Do()
        {
            Controller.Instance.Execute(() => _pw.ShowDialog());
        }
    }
}
